package cn.gok.springbootdemo05thymeleaf.resover;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
    自定义的国际化语言解析器，能进行中英文切花
 */
public class MyLocalResover implements LocaleResolver {

    /**
     在当前方法中配置国家化信息解析

     */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取语言类型参数值 zh_CN 或者 en_US
        String l = request.getParameter("l");

        //如果什么值都没有，则使用默认值
        Locale locale = Locale.getDefault();

        if(!StringUtils.isEmpty(l)) {
            String[] strings = l.split("_");
            locale = new Locale(strings[0], strings[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
